/**********************************************************
 * File
 *	options.js
 * Description
 *	Handles events from the options dialog.
 *********************************************************/

/*
 * Name: onOpenOptionsDialog()
 * Desc: Displays the common passwords list when the options dialog is opened.
 */
badpass.onOpenOptionsDialog = function() {
	//Get the strings bundle and preferences manager
	this.strings = document.getElementById("badpass-strings");
	this.prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	
	//Get the list of common passwords
	var passwords = badpass.getCommonPasswords();
	
	//Display the passwords
	var list = document.getElementById("lstcommonpasswords");
	var vulgarDefaults = {'asshole':'','bigcock':'','bigdick':'','bigtits':'','bitch':'','bitches':'','blowjob':'','boobs':'','cock':'','cumming':'','cumshot':'','cunt':'','dick':'','erotic':'','fuck':'','fucked':'','fucker':'','fucking':'','fuckme':'','fuckyou':'','horney':'','horny':'','mistress':'','naked':'','nipple':'','nipples':'','panties':'','penis':'','porn':'','porno':'','pussies':'','pussy':'','sexsex':'','sexy':'','shit':'','slut':'','suckit':'','tits':'','vagina':''};
	for(var i = 0; i < passwords.length; ++i) {
		//Ignore default vulgar passwords if the option is enabled (enabled by default)
		if(this.prefManager.getBoolPref("extensions.badpass.hidevulgardefaults") == true) {
			//Skip this loop iteration if the current password is in the vulgar default passwords object
			if(passwords[i] in vulgarDefaults) { continue; }
		}
		
		//Add the password to the list
		var newRow = document.createElement("listitem");
		newRow.setAttribute("label", passwords[i]);
		list.appendChild(newRow);
	}
}

/*
 * Name: handleAddPassword()
 * Desc: Allows the user to enter a string to be added to the common passwords
 *		 database.
 */
badpass.handleAddPassword = function() {
	//Show an input box where the user can enter a password to add
	var password = badpass.showInputBox("Add password", "Password: ");
	
	//Add the password to the list
	var list = document.getElementById("lstcommonpasswords");
	for(var i = 0; i < list.childNodes.length; ++i) {
		if(password < list.childNodes[i].getAttribute("label")) {
			list.insertItemAt(i, password, password);
			break;
		}
	}
	badpass.addPassword(password);
}

/*
 * Name: handleRemovePassword()
 * Desc: Allows the user to remove passwords from the common passwords database.
 */
badpass.handleRemovePassword = function() {
	//Remove the selected passwords from the database and dialog
	var list = document.getElementById("lstcommonpasswords");
	var count = list.selectedCount;
	while(count--) {
		var item = list.selectedItems[0];
		list.removeItemAt(list.getIndexOfItem(item));
		badpass.deletePassword(item.getAttribute("label"));
	}
}
